<template>
    <div>
        <tnxel-alert>建模依赖库，是指根据依赖的其它建模应用生成的依赖库。其内容可能变动，所以需进行同步管理。</tnxel-alert>
        <el-table :data="tableDataList" border empty-text="没有建模类型的依赖库">
            <el-table-column label="序号" type="index" width="60" align="center"/>
            <el-table-column label="应用" align="center" width="100">
                <template #default="{row}">
                    <span>{{ row.app.name }}</span>
                    <span class="text-placeholder ms-2" v-if="row.app.caption && row.app.caption !== row.app.name">
                    ({{ row.app.caption }})
                </span>
                </template>
            </el-table-column>
            <el-table-column label="建模依赖库">
                <template #default="{row}">
                    <div class="flex-v-center flex-nowrap" :title="row.name">
                        <NodeIcon type="lib"/>
                        <span>{{ row.simpleName }}</span>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="修改时间" prop="modelingSaveTime" width="170" align="center"/>
            <el-table-column label="同步时间" width="170" align="center">
                <template #default="{row}">
                <span :class="{'text-warning': row.syncable, 'text-success':!row.syncable}">
                    {{ row.modelingSyncTime }}
                </span>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="60" align="center">
                <template #default="{row}">
                    <tnxel-button type="primary" link v-if="row.syncable" @click="toSync(row)">同步</tnxel-button>
                </template>
            </el-table-column>
        </el-table>
    </div>

</template>

<script>
import tnx from '@/tnx';
import DependencyLibType from '@/logic/modeling/enums/DependencyLibType.js';
import PersistUtil from '@/logic/modeling/util/persist.js';

import NodeIcon from './components/NodeIcon.vue';

export default {
    components: {NodeIcon},
    props: {
        appName: String,
    },
    data() {
        return {
            model: {},
        };
    },
    computed: {
        project() {
            return tnx.app.global.modelingProject;
        },
        tableDataList() {
            let list = [];
            for (let app of this.project.apps) {
                if (!this.appName || app.name === this.appName) {
                    for (let dependency of app.dependencies) {
                        if (DependencyLibType.MODELING.equals(dependency.type)) {
                            list.push(Object.assign({}, dependency, {
                                app: app,
                                simpleName: dependency.artifactId + ':' + dependency.version,
                                name: dependency.getName(),
                                modelingSaveTime: dependency.getModelingSaveTime()?.formatDateTime(),
                                modelingSyncTime: dependency.modelingSyncTime?.formatDateTime(),
                                syncable: dependency.needsSync(),
                            }));
                        }
                    }
                }
            }
            return list;
        },
    },
    methods: {
        dialog() {
            return {
                title: '建模依赖库管理',
                width: 800,
                buttonText: '关闭',
            }
        },
        toSync(row) {
            tnx.confirm('确定要同步 ' + row.simpleName + ' 吗？').then(yes => {
                if (yes) {
                    tnx.showLoading();
                    let app = row.app;
                    let dependency = app.dependencies.find(d => d.matches(row));
                    PersistUtil.loadApp(dependency.modelingPath).then(dependencyApp => {
                        dependency.classes.length = 0;
                        for (let origin of dependencyApp.classes) {
                            dependency.pushClass(origin);
                        }
                        dependency.modelingSyncTime = new Date();
                        app.syncReferences();
                        PersistUtil.saveApp(app).then(() => {
                            row.modelingSyncTime = dependency.modelingSyncTime.formatDateTime();
                            tnx.toast('同步完成');
                        });
                    });
                }
            });
        },
    }
}
</script>

<style lang="scss" scoped>

</style>
